package com.bot.adms.controller.ad;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.bot.adms.model.bo.AdInfoBo;
import com.bot.adms.model.vo.AdInfoVo;
import com.bot.adms.service.impl.AdServiceImpl;

import javax.servlet.ServletOutputStream;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * 广告列表查询控制器
 * 根据条件查询广告列表信息，支持标题和状态筛选
 * 请求路径: /getAdList
 * 请求方式: GET
 * @author FangZ
 */
@WebServlet("/GetAdList")
public class GetAdListServlet extends HttpServlet {
    // 将AdServiceImpl声明为类成员变量
    private AdServiceImpl adService;
    
    // 在构造方法中初始化AdServiceImpl
    public GetAdListServlet() {
        this.adService = new AdServiceImpl();
    }
    
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        //修改字符集
        req.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8");
        resp.setContentType("text/html;charset=UTF-8");

        //响应体创建
        JSONObject jsonObject = new JSONObject();
        boolean success = false;
        int code = 500;
        String msg = "查询失败";
        JSONObject data = new JSONObject();

        try {
            //1.从请求参数中获取搜索条件
            String title = req.getParameter("title");
            String statusStr = req.getParameter("status");
            Integer status = null;
            
            //2.如果状态参数不为空，则转换为Integer类型
            if (statusStr != null && !statusStr.trim().isEmpty()) {
                status = Integer.parseInt(statusStr);
            }

            //3.创建AdInfoBo对象用于封装查询条件
            AdInfoBo adInfoBo = new AdInfoBo();
            adInfoBo.setAdTitle(title);
            adInfoBo.setStatus(status);
            
            //4.调用业务逻辑，使用类成员变量
            //5.调用服务层方法查询广告列表
            List<AdInfoVo> adList = adService.getAdList(adInfoBo);

            //6.响应结果
            if (adList != null) {
                success = true;
                code = 200;
                msg = "查询成功";
                data.put("adList", adList);
            } else {
                success = false;
                code = 500;
                msg = "未找到相关数据";
            }
        } catch (NumberFormatException e) {
            success = false;
            code = 500;
            msg = "状态格式错误，必须为数字";
        } catch (Exception e) {
            success = false;
            code = 500;
            msg = "查询列表失败: " + e.getMessage();
        }
        
        //组装JSON
        jsonObject.put("success", success);
        jsonObject.put("code", code);
        jsonObject.put("msg", msg);
        jsonObject.put("data", data);
        //转换成JSON字符串
        String json = JSON.toJSONString(jsonObject);

        //响应字符编码为UTF-8
        resp.setCharacterEncoding("utf-8");
        //设置响应内容类型为JSON格式
        resp.setContentType("application/json;charset=utf-8");
        //获取输出流并将JSON数据写入响应
        ServletOutputStream outputStream = resp.getOutputStream();
        outputStream.write(json.getBytes("UTF-8"));
        outputStream.flush();
        outputStream.close();
    }
}